import * as React from 'react'
import { OperatorKey, field_allow_operator } from './operators'

export type FieldType = keyof typeof field_allow_operator
/** 表达式基本关系 */
export enum RelationType { AND, OR }

export interface Field<T extends React.Key = FieldType> {
    name: string
    title?: string
    type: T
}
export interface Operator<OP extends React.Key = OperatorKey> {
    key: OP
    label: React.ReactNode
    /** 鼠标移入展示 */
    title?: string
    /** FormItem内放置元素 */
    component?: React.ComponentType<{
        fieldName?: string,
        fields?: Field<any>[],
        value?: any,
        onChange?: (value: any) => void,
        [k: string]: any,
    }>
}

export interface ExpressionItem<OP extends React.Key = OperatorKey> {
    field: string,
    op: OP,
    value?: any,
}
/** 表达式结构 */
export type Expression<OP extends React.Key = OperatorKey> = {
    relation: RelationType,
    items: (Expression<OP> | ExpressionItem<OP>)[]
} | ExpressionItem<OP>

/** 整体表达式组件Props定义 */
export interface ExpressionProps<OP extends React.Key, T extends React.Key> {
    readonly fields: Field<T>[]
    /** 所有支持的操作 */
    readonly op_map?: Record<OP, Operator<OP>>
    /** 各种字段类型支持的操作 */
    readonly type_map?: Record<T, OP[]>
    value?: Expression<OP>
    onChange?: (value: Expression<OP>) => void
    /**
     * 最多支持层级
     * @default 2
     * */
    deepth?: number
    i18n?: typeof I18N
}

export const I18N = {
    exp_item: '条件',
    exp_group: '表达式',
    op_del: '删除',
    op_del_confirm: '确定删除{}?',
    op_add: '+ 添加',
}

/** 单行表达式组件Props定义 */
export interface ExpressionLineProps<OP extends React.Key = any, T extends React.Key = any> {
    readonly fields: Field<T>[]
    /** 所有支持的操作 */
    readonly op_map?: Record<OP, Operator<OP>>
    /** 各种字段类型支持的操作 */
    readonly type_map?: Record<T, OP[]>
    value: ExpressionItem<OP>
    onChange: (value?: ExpressionItem<OP>) => void
    onDelete: () => void
}